#ifndef _TAIIC_RKISP_H
#define _TAIIC_RKISP_H

#include <linux/videodev2.h>
#include "uAPI2/rk_aiq_user_api2_imgproc.h"
#include "uAPI2/rk_aiq_user_api2_sysctl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h> /* getopt_long() */
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <dlfcn.h>
#include <signal.h>
#include <dirent.h>

#include "taiic_rkisp.h"
#include <termios.h>

#include <list>
#include <vector>
#include <string>
#include <algorithm>

#include "rk_debug.h"


static int silent;

#define DBG(...) do { if(!silent) printf(__VA_ARGS__); } while(0)
#define ERR(...) do { printf(__VA_ARGS__); } while (0)

#define FMT_NUM_PLANES 1

#define BUFFER_COUNT 3
#define IQFILE_PATH_MAX_LEN 256

typedef struct _taiic_rkisp_context {
    char                    out_file[255];
    char                    dev_name[255];
    char                    sns_name[32];
    int                     width;
    int                     height;
    int                     format;
    int                     fd;
    enum v4l2_buf_type      buf_type;
    struct buffer           *buffers;
    unsigned int            n_buffers;
    int                     frame_count;
    FILE                    *fp;
    rk_aiq_sys_ctx_t*       aiq_ctx;
    int                     rkaiq;
    int                     writeFile;
    int                     outputCnt;
    int                     skipCnt;
    int                     ctl_type;
    char                    iqpath[255];
} taiic_rkisp_s;



struct buffer {
    void *start;
    size_t length;
    int export_fd;
    int sequence;
};

enum TEST_CTL_TYPE {
    TEST_CTL_TYPE_DEFAULT,
    TEST_CTL_TYPE_REPEAT_INIT_PREPARE_START_STOP_DEINIT,
    TEST_CTL_TYPE_REPEAT_START_STOP,
    TEST_CTL_TYPE_REPEAT_PREPARE_START_STOP,
};

#define CLEAR(x) memset(&(x), 0, sizeof(x))

void errno_exit(taiic_rkisp_s *ctx, const char *s);
char* get_sensor_name(taiic_rkisp_s* ctx);
char* get_dev_name(taiic_rkisp_s* ctx);
int xioctl(int fh, int request, void *arg);


void init_rkisp_ctx(taiic_rkisp_s *ctx);


void init_rkisp_routine(taiic_rkisp_s *ctx);
void open_device(taiic_rkisp_s *ctx);
void init_device(taiic_rkisp_s *ctx);
void init_mmap(int pp_onframe, taiic_rkisp_s *ctx);
void start_capturing(taiic_rkisp_s *ctx);



int read_frame(taiic_rkisp_s *ctx);
void process_image(const void *p, int sequence, int size, taiic_rkisp_s *ctx);
void stop_capturing(taiic_rkisp_s *ctx);
void close_device(taiic_rkisp_s *ctx);
void uninit_device(taiic_rkisp_s *ctx);
void deinit_rkisp(taiic_rkisp_s *ctx);


#endif //_TAIIC_RKISP_H
